/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entities.TraceState;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;


/**
 *
 * @author Eduardo
 */

public class TraceStateDAO extends GeneralDAO {

    public TraceState searchById(EntityManager em,Integer id) {
        Query q = em.createQuery("SELECT ts FROM TraceState AS ts "
                + "  WHERE ts.idTraceState = :id");
        q.setParameter("id", id);
        List<TraceState> results = q.getResultList();
        if (results == null) {
            return null;  // No encontrado
        } else {
            return results.get(0);  // Devuelve el encontrado
        }
    }
    
    
    
    
    public TraceState searchByTraceName(EntityManager em,String traceName) {
        Query q = em.createQuery("SELECT ts FROM tracestate AS ts "
                + "  WHERE ts.nameState = :traceState");
        q.setParameter("traceState", traceName);
        List<TraceState> results = q.getResultList();
        if (results == null) {
            return null;  // No encontrado
        } else {
            return results.get(0);  // Devuelve el encontrado
        }
    }

    public List<TraceState> validate(EntityManager em,String state) {
        Query q = em.createQuery("SELECT object(ts) FROM tracestate AS ts "
                + "  WHERE ts.idTraceState = :id");
        q.setParameter("id", state);
        List<TraceState> resultados = q.getResultList();
        if (resultados == null) {
            return null;  // No encontrado
        } else if (resultados.size() != 1) {
            return null; // No encontrado
        } else {
            return q.getResultList();  // Devuelve el encontrado
        }
    }


    public List<TraceState> searchAll(EntityManager em) {
        Query q = em.createQuery("SELECT object(ts) FROM tracestate as ts");
        return q.getResultList();
    }

}